﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;


namespace ERPStudio
{
   
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Trace.AutoFlush = true;
            try
            {
                Stream myFile = null;
                while (myFile == null)
                {
                    try
                    {
                        myFile = File.OpenWrite(Directory.GetCurrentDirectory() + "\\Trace.Log");
                    }
                    catch { }
                }
                myFile.Position = myFile.Length;
                TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile);
                Trace.Listeners.Add(myTextListener);
            }
            catch (Exception)
            {

            }
            Trace.WriteLine("[" + DateTime.Now.ToString() + "]" + "Start ERPStudioLogSystem");
            FrmMainMdi mainFrm = new FrmMainMdi();
        
            
            SplashFrm.ShowSplashScreen();
            Application.DoEvents();
            //verifica existenta directorului Module
            string catCurent = Directory.GetCurrentDirectory();
            String fileAU = catCurent.ToLower() + @"\autoupdate.new";
            String fileAX = catCurent.ToLower() + @"\autoupdate.exe";
            if (File.Exists(fileAU))
            {
                Thread.Sleep(10000);
                if (File.Exists(fileAX))
                {
                    Stream myFile = null;
                    while (myFile == null)
                    {
                        try
                        {
                            myFile = File.OpenWrite(fileAX);
                        }
                        catch { }
                    }
                    myFile.Close();

                    File.Delete(fileAX);
                }
                File.Move(fileAU, fileAX);
                Trace.WriteLine("[" + DateTime.Now.ToString() + "]" + "Redenumire autoupdate.exe");
            }
            //try
            //{
            Application.Run(mainFrm);
            //}
            //catch(Exception ex)
            //{
            //    Trace.WriteLine("[" + DateTime.Now.ToString() + "]" + ex.ToString());
            //    MessageBox.Show(ex.Message);
            //    //Application.Run(mainFrm);
            //    return;
            //}

            
        }


        private static void ShowAssemblies(object sender, AssemblyLoadEventArgs e)
        {
            // Store name of assembly in the queue
            //SplashFrm.AsmLoads.Enqueue(e.LoadedAssembly.GetName().Name);
        }

    }
}